﻿namespace BigNote
{
    using System.Linq;
    using System.Windows;
    using System.Reflection;
    using System.Windows.Input;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int colorIndex = 0;
        private int fontIndex = 0;
        private PropertyInfo[] colorProperties;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindowLoaded;
            KeyDown += MainWindowKeyDown;
            colorProperties = typeof(Colors).GetProperties();
            settingsExpander.Collapsed += SettingsCollapsed;
        }

        private void SettingsCollapsed(object sender, RoutedEventArgs e)
        {
            mainTextBox.Focus();
        }

        protected override void OnActivated(System.EventArgs e)
        {
            base.OnActivated(e);
            mainTextBox.Focus();
            settingsExpander.Opacity = 100;
        }

        protected override void OnDeactivated(System.EventArgs e)
        {
            base.OnDeactivated(e);
            settingsExpander.Opacity = 0;
        }

        void MainWindowKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                ClearCurrentText();
            }

            if (e.Key == Key.F2)
            {
                ToggleFont();
            }

            if (e.Key == Key.F3)
            {
                ToggleColor();
            }
        }

        private void ClearCurrentText()
        {
            mainTextBox.Text = "";
            mainTextBox.Focus();
        }

        private void ToggleColor()
        {
            if (colorIndex < colorProperties.Length - 1)
            {
                colorIndex++;
            }
            else
            {
                colorIndex = 0;                
            }
            mainTextBox.Foreground = new SolidColorBrush((Color)colorProperties[colorIndex].GetValue(null, null));
        }

        private void ToggleFont()
        {
            mainTextBox.FontFamily = Fonts.SystemFontFamilies.Skip(fontIndex).First();
            fontIndex++;
            if (fontIndex >= Fonts.SystemFontFamilies.Count)
            {
                fontIndex = 0;
            }
        }

        void MainWindowLoaded(object sender, RoutedEventArgs e)
        {
            mainTextBox.Focus();
        }
    }
}
